So, hier mein Code für die Wandlung von 32-Bit HEX nach Dezimal und Dezimal zurück nach HEX aus "go lucky".
Ich habe dort maximal 10 Stellen Dezimal gehabt und daher auch nicht weiter berechnet.
Vor dem Aufruf muss jeweils die value (in low...high) gefüllt werden und man erhält dann in result das Ergebnis (in low...high)
//--------------------------
; converts 10 digits (32 bit values has max. 10 decimal digits)
.local hex2dec32 ;result is low...high
ldx #0
l3 jsr div10
sta result,x
inx
cpx #10
bne l3
rts
; divides a 32 bit value by 10
; remainder is returned in akku
div10
ldy #32 ; 32 bits
lda #0
clc
l4 rol
cmp #10
bcc skip
sbc #10
skip rol value
rol value+1
rol value+2
rol value+3
dey
bpl l4
rts
value .he 00 00 00 00
result .byte 0,0,0,0,0,0,0,0,0,0
.endl
//--------------------------
.local dec2hex32 ;result is low...high
;reset result
lda #0
sta result
sta result+1
sta result+2
sta result+3
ldx #0
lp lda value,x ;immer 0-9
sta Num1
lda #0
sta Num1+1
sta Num1+2
sta Num1+3
lda h0,x ;x 10, x 100, x 1000, etc
sta Num2
lda h1,x
sta Num2+1
lda h2,x
sta Num2+2
lda h3,x
sta Num2+3
stx m_x
jsr MulDWord
;Ergebnis addieren zu result
lda Num3
sta add32.v0
lda Num3+1
sta add32.v0+1
lda Num3+2
sta add32.v0+2
lda Num3+3
sta add32.v0+3
lda result
sta add32.v1
lda result+1
sta add32.v1+1
lda result+2
sta add32.v1+2
lda result+3
sta add32.v1+3
jsr add32
lda add32.e
sta result
lda add32.e+1
sta result+1
lda add32.e+2
sta result+2
lda add32.e+3
sta result+3
inc m_x
ldx m_x
cpx #10
beq @+
jmp lp
@ rts
value .byte 0,0,0,0,0,0,0,0,0,0
result .he 00 00 00 00
h0 .he 01 0a 64 e8 10 a0 40 80 00 00
h1 .he 00 00 00 03 27 86 42 96 e1 ca
h2 .he 00 00 00 00 00 01 0f 98 f5 9a
h3 .he 00 00 00 00 00 00 00 00 05 3b
m_x .he 00
.endl
//--------------------------
.local add32 ;v0+v1=e
clc
lda v0
adc v1
sta e
lda v0+1
adc v1+1
sta e+1
lda v0+2
adc v1+2
sta e+2
lda v0+3
adc v1+3
sta e+3
rts
v0 .he 00 00 00 00
v1 .he 00 00 00 00
e .he 00 00 00 00
.endl
//--------------------------
;code by flashjazzcat
; Divide Num1 by Num2 and place result in Num1
; Remainder is left in Num3, which MUST be immediately after Num1 in RAM
; Optimised for size rather than speed
;
.local DivDWord ; Divide Num1 by Num2 and place result in Num1
lda #0 ;preset remainder to 0
ldx #3
@
sta Num3,x
dex
bpl @-
ldy #32 ; repeat for each bit
DivLoop
clc ; clear carry so we can use ROL throughout
ldx #248 ; dividend lb & hb*2, msb -> Carry
@
rol.w Num1-248,x ; .w ensures ZP addressing is never used
inx
bne @-
sec
ldx #252
@
lda.w Num3-252,x
sbc.w Num2-252,x
pha
inx
bne @-
bcs @+
pla
pla
pla
pla
bcc Skip
@
ldx #3
@
pla
sta Num3,x
dex
bpl @-
inc Num1 ; and increment result because divisor fit
Skip
dey
bne DivLoop
rts
;----------------
.endl
Definition von Num:
;----- for calculations
Num1 ;.ds 4
.he 00 00 00 00
Num3 ;.ds 8 ; must follow Num1 for Div to work
.he 00 00 00 00 00 00 00 00
Num2 ;.ds 4
.he 00 00 00 00
Sehr wahrscheinlich nicht die beste Lösung - aber es hat funktioniert für mich und daher bin ich zufrieden.
PP´s of STARSOFTBerlin____github|meine Webseite|Demozoo